﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace truongServer
{
    public partial class dangnhapAdminServer : Form
    {
        adminLibaryServer.FACEDE.adminFac adFace;
        updateAdmin update;
        
        public dangnhapAdminServer(adminLibaryServer.FACEDE.adminFac adFace,updateAdmin update)
        {
            this.adFace = adFace;
            this.update = update;
            InitializeComponent();
            simpleButton2_Click(null, null);
           
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtUsername.Text.Trim()) && !string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                List<Entities.tblAdmin> admins = adFace.viewAllAdmin();
                int y = 0;
                foreach (Entities.tblAdmin admin in admins)
                {
                    if (admin.IdTruong.ToString() == comboBox1.SelectedValue.ToString())
                    {

                        if (admin.Email == txtUsername.Text.Trim() && admin.Password == txtPassword.Text.Trim() && admin.Enable == "True")
                        {
                            y = 1;
                            update.idTruong = int.Parse(comboBox1.SelectedValue.ToString());
                            update.username = txtUsername.Text.Trim();
                            update.password = txtPassword.Text.Trim();
                            break;
                        }
                       
                    }
                }
                if (y > 0) { this.DialogResult = DialogResult.OK; }
                else MessageBox.Show("Thông tin quản trị sai.");
            }
            else MessageBox.Show("Vui lòng nhập đầy đủ thông tin.");
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //try
            //{
            List<Entities.tblTruongHoc> truongs = adFace.viewAllTruong();
            Dictionary<int, string> dic = new Dictionary<int, string>();
            foreach (Entities.tblTruongHoc truong in truongs)
            {
                dic.Add(truong.Stt, truong.TenTruong);
            }
            comboBox1.DataSource = null;
            comboBox1.DataSource = new BindingSource(dic, null); ;
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember = "Key";
           
                //comboBox1.DataSource = truong;
                //comboBox1.DisplayMember = "TenTruong";
                //comboBox1.ValueMember = "Stt";
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("Mất kết nối server");
            //    this.DialogResult = DialogResult.No;
            //}
        }
    }
}
